/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless;

import entity.Stakeholder;
import entity.Transactions;
import java.sql.Timestamp;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Jacko
 */
@Stateful
public class TransactionsSession implements TransactionsSessionLocal {

    @PersistenceContext
    private EntityManager em;

    /*private Long getServicePlan(Long stakeholderID){
    Stakeholder svcPlan = em.find(Stakeholder.class, stakeholderID); 
    
    return svcPlan.getServicePlan().getSvcPlanID(); 
    }*/
    private Stakeholder getStakeholderID(Long stakeholderID) {
        Stakeholder stakeholder = em.find(Stakeholder.class, stakeholderID);

        return stakeholder;
    }

    public Long addTransaction(Timestamp transDate, String transName, String transDesc,
           int transDiscount, String status, Long stakeholderID) {
            //added transname and discount
        double calAmount;

        Stakeholder stakeholder = getStakeholderID(stakeholderID);
        calAmount = calculateAmount(stakeholderID);

        Transactions transaction = new Transactions();
        transaction.setTransDate(transDate);
        transaction.setTransName(transName); //tony
        transaction.setTransDesc(transDesc);
        transaction.setTransDiscount(transDiscount); //tony
        transaction.setTransAmt(calAmount);
        transaction.setStatus(status);

        transaction.setStakeholder(stakeholder);

        em.persist(transaction);

        return transaction.getTransID();
    }

    public int modifyTransaction() {
        return 0; 
    }

    public List<Transactions> getMyTransactions() {
        Query query = em.createQuery("SELECT t FROM Transactions t");
        return query.getResultList();
    }

    public List<Transactions> getAllTransactions() {
        Query query = em.createQuery("SELECT t FROM Transactions t");
        return query.getResultList();
    }

    private double calculateAmount(Long stakeholderID) {

        double mthFee;
        double comm1;
        double comm2;
        double initFee;
        double perUnitFee;
        int numofAdClicks;
        double revenue;
        double amountToBill;

        Stakeholder svcPlan = em.find(Stakeholder.class, stakeholderID);
        mthFee = svcPlan.getServicePlan().getSvcPlanMthFee();
        comm1 = svcPlan.getServicePlan().getSvcPlancomm1();
        comm2 = svcPlan.getServicePlan().getSvcPlancomm2();
        initFee = svcPlan.getServicePlan().getInitialFee();
        perUnitFee = svcPlan.getServicePlan().getPerUnitFee();

        numofAdClicks = svcPlan.getNumberOfAdClicks();
        revenue = svcPlan.getRevenue();

        amountToBill = mthFee + ((comm1 / 100) * revenue) + ((comm2 / 100) * revenue)
                + initFee + (perUnitFee * numofAdClicks);

        return amountToBill;
    }

    /* public Long generateInvoice() {
    
    }
    
    public List<Transactions> getMyInvoice() {
    throw new UnsupportedOperationException("Not supported yet.");
    }*/
}
